package com.mcptt.db.op;

import java.util.List;
import java.util.Vector;

import com.cloudptt.api.product.log.Log;
import com.mcptt.db.DBProvider;
import com.mcptt.db.DbConst;
import com.mcptt.db.model.MapPointInfo;
import com.mcptt.db.model.OnlineInfo;

import android.content.ContentResolver;
import android.content.Context;
import android.database.Cursor;

public class MapDbOperator {

	public static String THIS_FILE = "MapDbOperator";
	private Context ctx = null;

	public MapDbOperator(Context ctx)
	{
		this.ctx = ctx;
	}
	public void insertOrUptNewPoint(MapPointInfo point)
	{
		Log.i(THIS_FILE,"insertOrUptNewPoint point =  " + point);

		ContentResolver cr = ctx.getContentResolver();

		StringBuffer strBuf = new StringBuffer(); 
		//strBuf.append(DbConst.COL_ReceiverNum).append(" =? and ");
		strBuf.append(DbConst.COL_SenderId).append(" =? ");

		//String receiverNum = point.getReceiver().getMcId();
//		String senderNum = point.getSenderId();
//		String[] value = new  String[]{senderNum};

		cr.insert(DbConst.MAP_POINT_URI, point.getContentValues());

		//cr.delete(DbConst.LOGIN_URI, null, null);
		cr.insert(DbConst.LOGIN_URI, point.getLoginContentValues());

//		ContentValues val = point.getTrackContentValues();
//		Log.i(THIS_FILE,"insertOrUptNewPoint val =  " + val);
//		cr.insert(DbConst.LOCATION_TRACK_URI, val);
	}

	public void insertOrUptOnline(OnlineInfo info)
	{
		Log.i(THIS_FILE,"insertOrUptOnline info =  " + info);

		ContentResolver cr = ctx.getContentResolver();

		StringBuffer selection = new StringBuffer();
		String num = info.getSenderId();
		selection.append(DbConst.COL_SenderId).append(" = ").append(num);

		//String receiverNum = point.getReceiver().getMcId();
//		String senderNum = point.getSenderId();
//		String[] value = new  String[]{senderNum};
		if(info != null
			&& info.getStatue() == 1) {
			int ret = cr.update(DbConst.LOGIN_ID_URI_BASE, info.getContentValues(), selection.toString(), null);
			if (ret <= 0) {
				cr.insert(DbConst.LOGIN_URI, info.getContentValues());
			}
		}
		else
		{
			cr.delete(DbConst.LOGIN_ID_URI_BASE,selection.toString(), null);
		}


		//cr.delete(DbConst.LOGIN_URI, null, null);
//		cr.insert(DbConst.LOGIN_URI, point.getLoginContentValues());

//		ContentValues val = point.getTrackContentValues();
//		Log.i(THIS_FILE,"insertOrUptNewPoint val =  " + val);
//		cr.insert(DbConst.LOCATION_TRACK_URI, val);
	}
	
//	public void insertOrUptAI()
//	{
//		Log.i(THIS_FILE,"insertOrUptNewPoint point =  " + point);
//
//		ContentResolver cr = ctx.getContentResolver();
//
//		StringBuffer strBuf = new StringBuffer(); 
//		//strBuf.append(DbConst.COL_ReceiverNum).append(" =? and ");
//		strBuf.append(DbConst.COL_SenderId).append(" =? ");
//
//		//String receiverNum = point.getReceiver().getMcId();
////		String senderNum = point.getSenderId();
////		String[] value = new  String[]{senderNum};
//
//		cr.insert(DbConst.MAP_POINT_URI, point.getContentValues());
//
//		//cr.delete(DbConst.LOGIN_URI, null, null);
//		cr.insert(DbConst.LOGIN_URI, point.getLoginContentValues());
//
////		ContentValues val = point.getTrackContentValues();
////		Log.i(THIS_FILE,"insertOrUptNewPoint val =  " + val);
////		cr.insert(DbConst.LOCATION_TRACK_URI, val);
//	}
//	

	

	public List<MapPointInfo> getMapPointInfoList()
	{
		ContentResolver cr = ctx.getContentResolver();
		List<MapPointInfo> list = new Vector<MapPointInfo>();
//		String selection = DbConst.COL_ReceiverNum + "=" + groupNum;
		Cursor cursor = null;
		try 
		{
			cursor = cr.query(DbConst.MAP_POINT_URI,
					DBProvider.MAP_POINT_FULL_PROJECTION, null,
					null, DbConst.COL_Name );

			if (cursor == null
					|| cursor.getCount() <= 0)
			{
				return list;
			}
			while (cursor.moveToNext()) 
			{  
				MapPointInfo info = new MapPointInfo(cursor);  
				list.add(info);
			} 
		} 
		catch (Exception e) 
		{
			// TODO: handle exception
			Log.debug(this.getClass(), "getMapPointInfoList e = " + e);
			e.printStackTrace();
		}
		finally
		{
			if(cursor != null)
			{
				cursor.close();
				cursor = null;
			}
		}

		return list;
	}
	public void cleanAllLogin()
	{
		Log.i(THIS_FILE,"cleanAllLogin");
		ContentResolver cr = ctx.getContentResolver();
		cr.delete(DbConst.LOGIN_URI, "1=1", null);
		cr.delete(DbConst.MAP_POINT_URI, "1=1", null);
	}
}
